/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

/**
 * Servlet implementation class for Servlet: GetConceptTreeNode
 * 
 * @web.servlet name="GetConceptTreeNode" display-name="GetConceptTreeNode"
 * 
 * @web.servlet-mapping url-pattern="/GetConceptTreeNode"
 * 
 */
public class GetConceptTreeNode extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public GetConceptTreeNode() {
		super();
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	   throws ServletException, IOException {
	    doPost(req, resp);
	}
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
//		if ( !Login.LOGGED_IN.getValue(request) ) {			
//			response.sendRedirect( request.getContextPath() + "/login");
//			return;
//		}
		
		// Get the data parameter sent by dojo
		try{
		String data = request.getParameter("data");	
		String module = request.getParameter("module");
		String currnodes = request.getParameter("currnodes");
		module = URLDecoder.decode(module, "UTF-8");
		String isinclass = request.getParameter("isinclass");
		boolean inclass = false;
		// Gather the objectid from the data parameter string.
		StringBuffer json = new StringBuffer();
		String OBJECTID = "\"objectId\":\"";
		int start_idx = data.indexOf(OBJECTID) + OBJECTID.length();
		int end_idx = data.indexOf('\"', start_idx);
		String objectId = data.substring(start_idx, end_idx);	
		String currnodes_array[] = currnodes.split(controller.HelperFunctions.DEFAULTPLITTER);
		ArrayList<URI> currnodes_list = new ArrayList<URI>();
		if (currnodes_array != null && currnodes_array.length > 0){
			for (int i=0; i<currnodes_array.length; i++){
				if (currnodes_array[i] != null && !currnodes_array[i].equals("")){
					currnodes_list.add(new URIImpl(model.Vocabulary.NAMESPACE.stringValue()+currnodes_array[i] ));
				}
			}
		}
		objectId = URLDecoder.decode(objectId, "UTF-8");
		
		URI uri = new URIImpl(model.Vocabulary.NAMESPACE+objectId);
        if (isinclass.equals("true"))
        	inclass = true;
		json = controller.HelperFunctions.getSubNodesForTree(uri, false, true, module, inclass, controller.HelperFunctions.MAXPARALLELTREENODES, currnodes_list);
		if (json == null)
			json = new StringBuffer();
		
		// get the servlet response object here..
		response.setContentType("text/javascript");
		
			PrintWriter writer = response.getWriter();
			writer.println(json.toString());
			writer.close();
		} catch (IOException e) {e.printStackTrace();}
		
	}
}
